{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7b12c19c-84fc-471e-a721-fac76de9bb4e",
   "metadata": {},
   "source": [
    "特征编码，针对非数值类型的特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a705b822-441f-4198-bc48-ea391f676477",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame({'编号': [1, 2, 3, 4, 5], '城市': ['北京', '上海', '广州', '深圳', '北京']})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7ee8a48f-ea62-4005-97d8-1963f36bd64b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "le = LabelEncoder()\n",
    "label = le.fit_transform(df['城市'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "615018d8-f5b4-4102-927c-16cbf098fc8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>城市</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编号  城市\n",
       "0   1   1\n",
       "1   2   0\n",
       "2   3   2\n",
       "3   4   3\n",
       "4   5   1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['城市'] = label\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55daa499-4363-4257-aef8-0ce63547bed0",
   "metadata": {},
   "source": [
    "重复值 异常值 缺失值处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cddeb3cd-114c-437e-a8cb-b18e4f504631",
   "metadata": {},
   "source": [
    "drop_duplicates()可以函数删除重复行\n",
    "isnull()函数可以查看缺失值\n",
    "对于含有缺失值的数据 可以选择剔除 如果数据珍贵 可以采用填充的方法 用均值填充或者更高级的方法插值\n",
    "\n",
    "对于异常值 通过画箱线图，利用3cigema原则去除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9dc2c9d1-349d-4565-8c7f-21e8a2f26f65",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d25bab92-ea6f-495c-9ecc-c876284e086b",
   "metadata": {},
   "source": [
    "去除冗余特征\n",
    "一些特征的相关性强，只需保留一个即可，可以通过画热力图查看，皮尔曼 皮尔逊相关系数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db6d8021-b60a-487c-8a9a-44906c175d19",
   "metadata": {},
   "source": [
    "对于类别不平衡的数据在做模型训练时 需先做平衡化处理，防止模型倾向于某一类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cf33b67-8b29-47e2-8012-36260bac99fe",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
